package ar.com.e2solver.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import ar.com.e2solver.model.Puzzle;


public class SimplePuzzleView {
	
	private Collection<Puzzle> puzzles;
	private JFrame mainFrame;
	private JPanel panel;
	
	public SimplePuzzleView(Puzzle puzzle) {
		List<Puzzle> puzzles = new ArrayList<Puzzle>(1);
		puzzles.add(puzzle);
		
		initialize(puzzles);
	}
	
	public SimplePuzzleView(Puzzle puzzle, String title){
		List<Puzzle> puzzles = new ArrayList<Puzzle>(1);
		puzzles.add(puzzle);
		
		initialize(puzzles);
		mainFrame.setTitle(title);
	}
	
	public SimplePuzzleView(Collection<Puzzle> puzzles){
		initialize(puzzles);
	}
	
	private void initialize(Collection<Puzzle> puzzles) {
		this.puzzles = puzzles;
		
	    BorderLayout bl = new BorderLayout();
	    bl.setHgap(10);
		this.mainFrame = new JFrame("E2SolverView");
	    this.mainFrame.setLayout(bl);
	    this.panel = new JPanel();
		
	    draw();
	    centerFrame();
	}
	
	public void draw(){
		panel.removeAll();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		for (Puzzle puzzle : this.puzzles) {
			panel.add(new PuzzleView(puzzle));	
		}
	    
		this.mainFrame.add(panel, BorderLayout.CENTER);
		this.mainFrame.pack();
		this.mainFrame.setVisible(true);
	}
	
	public void update(Puzzle puzzle){
		this.puzzles.clear();
		this.puzzles.add(puzzle);
		draw();
	}
	

	private void centerFrame(){
	    Toolkit toolkit = Toolkit.getDefaultToolkit();
	    Dimension screenSize = toolkit.getScreenSize();

	    int x = (screenSize.width - this.mainFrame.getWidth()) / 2;
	    int y = (screenSize.height - this.mainFrame.getHeight()) / 2;

	    this.mainFrame.setLocation(x, y);		
	}

}
